package mongo

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/store"
	"fmt"
	"sync"
)

type datastore struct {
	// database instance
	db *helper.Mongo
}

func (d *datastore) Bim() store.BimStore {
	return newBimStore(d)
}

func (d *datastore) Workflow() store.WorkflowStore {
	return newWorkflowStore(d)
}

var (
	mongoFactory store.Factory
	once         sync.Once
)

// GetMongoFactory create mongo factory.
func GetMongoFactory() (store.Factory, error) {
	var err error
	var db *helper.Mongo

	once.Do(func() {
		db, err = server.Mongo()

		mongoFactory = &datastore{db}
	})

	if mongoFactory == nil || err != nil {
		return nil, fmt.Errorf("failed to get mysql store fatory, mysqlFactory: %+v, error: %w", mongoFactory, err)
	}
	return mongoFactory, nil
}
